import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Main {
    private static final String encoding = "UTF-8";
    private static final String localFilePath = "C://students.txt";
    private static final String localSavedPath = "D://Desktop//savedStudentList.txt";
    private static final String mathScoreUrl = "http://139.186.26.196/javaweb/data/math.txt";
    private static final String chineseScoreUrl = "http://139.186.26.196/javaweb/data/chinese.txt";
    private static final String englishScoreUrl = "http://139.186.26.196/javaweb/data/english.txt";

    private static final Map<String, Integer> chineseScoreHashSet = new HashMap<>();
    private static final Map<String, Integer> mathScoreHashSet = new HashMap<>();
    private static final Map<String, Integer> englishScoreHashSet = new HashMap<>();

    public static void main(String[] args) {
        List<Student> studentList = new ArrayList<>();
        StudentManager studentManager = StudentManager.getInstance();
        System.out.println("hello ZJY's homework!");
        readTxtFileFormUrl(chineseScoreUrl, chineseScoreHashSet);
        readTxtFileFormUrl(mathScoreUrl, mathScoreHashSet);
        readTxtFileFormUrl(englishScoreUrl, englishScoreHashSet);
        readTxtFile(studentList);
        studentManager.setStudentList(studentList);
        System.out.println("All student information has been registered");
        studentManager.printAll();
        studentManager.getStudentById("222020310042011");
        studentManager.findByScore("语文", 75, 90);
        studentManager.findByScore(77, 88);
        studentManager.sortByTotal();
        studentManager.saveAs(localSavedPath);
    }

    private static void readTxtFile(List<Student> studentList) {
        try {
            File file = new File(localFilePath);
            if (file.isFile() && file.exists()) { //判断文件是否存在
                InputStreamReader reader = new InputStreamReader(new FileInputStream(file), encoding);//考虑到编码格式
                BufferedReader bufferedReader = new BufferedReader(reader);
                String lineTxt;
                while ((lineTxt = bufferedReader.readLine()) != null) {
//                    读取每一行的学号，姓名等数据
                    if (!lineTxt.equals("")) {
                        String studentID = lineTxt.substring(0, 15);
                        studentList.add(new Student(studentID, lineTxt.substring(16), chineseScoreHashSet.get(studentID), mathScoreHashSet.get(studentID), englishScoreHashSet.get(studentID)));
                    }
                }
            } else {
                System.out.println("Cannot find students.txt");
            }
        } catch (Exception e) {
            System.out.println("Error reading file content");
            e.printStackTrace();
        }
    }

    private static void readTxtFileFormUrl(String scoreUrl, Map<String, Integer> scoreHashMap) {
        int httpResult;
        try {
            URL fileUrl = new URL(scoreUrl);
            URLConnection urlConnection = fileUrl.openConnection();
            urlConnection.connect();
            HttpURLConnection httpURLConnection = (HttpURLConnection) urlConnection;
            httpResult = httpURLConnection.getResponseCode();
            if (httpResult != HttpURLConnection.HTTP_OK) {
                System.out.println("Cannot connect");
            } else {
                InputStreamReader reader = new InputStreamReader(urlConnection.getInputStream(), encoding);
                BufferedReader bufferedReader = new BufferedReader(reader);
                String lineText;
                while ((lineText = bufferedReader.readLine()) != null) {
                    if (!lineText.equals("")) {
                        scoreHashMap.put(lineText.substring(0, 15), Integer.parseInt(lineText.substring(16)));
                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}